//
//  ClubBannarView.swift
//  77Club
//
//  Created by Apple on 2024/8/26.
//

import UIKit
import FSPagerView

class ClubHomeInfoTopView: UIView,FSPagerViewDelegate,FSPagerViewDataSource {

    var banner = FSPagerView()
    var bannarModel: [ClubHomeInfoBannerModel]? {
        didSet{
            self.pagerControl.numberOfPages = bannarModel?.count ?? 0
            banner.reloadData()
        }
    }
    // 懒加载滚动图片浏下标
    lazy var pagerControl:FSPageControl = {
        let pageControl = FSPageControl()
        //设置下标位置
        pageControl.contentHorizontalAlignment = .right
        pageControl.setImage("Home_bannar_nor".toImage, for: .normal)
        pageControl.setImage("Home_bannar_sel".toImage, for: .selected)
        pageControl.itemSpacing = 2
            
        return pageControl

    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(banner)
        addSubview(pagerControl)
        
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.register(ClubHomeInfoFSCell.self, forCellWithReuseIdentifier: "ClubHomeInfoFSCell")
        banner.automaticSlidingInterval = 3
        banner.interitemSpacing = 9.0.Scale
        banner.itemSize = CGSize(width: .screenWidth, height: 170.0.Scale)
        banner.removesInfiniteLoopForSingleItem = true
        
        banner.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        pagerControl.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-25.0.Scale)
            make.bottom.equalToSuperview().offset(-24.0.Scale)
            make.height.equalTo(4.0.Scale)
            make.width.equalTo(20.0.Scale)
        }
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return bannarModel?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "ClubHomeInfoFSCell", at: index) as! ClubHomeInfoFSCell
        cell.model = bannarModel?[index]
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int) {
            self.pagerControl.currentPage = index
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        var urlString = bannarModel?[index].link ?? ""
        let gid = bannarModel?[index].gid ?? 0
        if gid > 0 {
            let detailVC = ClubGameDetailViewController()
            detailVC.gameId = String(gid)
            UIApplication.topController()?.navigationController?.pushViewController(detailVC, animated: true)
        }else{
            if urlString.count > 0 {
                if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                    urlString = urlString + "?uid=\(uid)"
                }
                let webVC = ClubHiddenBavWebViewController()
                urlString = urlString + "&model=ios"
                webVC.urlString = urlString
                UIApplication.topController()?.navigationController?.pushViewController(webVC, animated: true)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class ClubHomeInfoFSCell:FSPagerViewCell {

    var bgImageView = UIImageView()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    
    var model:ClubHomeInfoBannerModel? {
        didSet {
            guard let md = model else { return }
            bgImageView.loadImage(url: md.image)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.layer.shadowColor = UIColor.clear.cgColor
        
        subviews{
            bgImageView.subviews{
                shadowView
            }
        }
        
        bgImageView.contentMode = .scaleAspectFill
        bgImageView.layer.cornerRadius = 8
        bgImageView.layer.masksToBounds = true
        
        bgImageView.top(8.0.Scale).leading(12.0.Scale).trailing(12.0.Scale).height(150.0.Scale)
        shadowView.fillContainer()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class ClubInfoTopView: UIView,FSPagerViewDelegate,FSPagerViewDataSource {

    var banner = FSPagerView()
    var bannarModel: [ClubHomeInfoBannerModel]? {
        didSet{
            self.pagerControl.numberOfPages = bannarModel?.count ?? 0
            banner.reloadData()
        }
    }
    // 懒加载滚动图片浏下标
    lazy var pagerControl:FSPageControl = {
        let pageControl = FSPageControl()
        //设置下标位置
        pageControl.contentHorizontalAlignment = .right
        pageControl.setImage("Home_bannar_nor".toImage, for: .normal)
        pageControl.setImage("Home_bannar_sel".toImage, for: .selected)
        pageControl.itemSpacing = 2
            
        return pageControl

    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        addSubview(banner)
        addSubview(pagerControl)
        
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.register(ClubInfoFSCell.self, forCellWithReuseIdentifier: "ClubInfoFSCell")
        banner.automaticSlidingInterval = 3
        banner.interitemSpacing = 9.0.Scale
        banner.itemSize = CGSize(width: .screenWidth, height: 170.0.Scale)
        banner.removesInfiniteLoopForSingleItem = true
        
        banner.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        pagerControl.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-25.0.Scale)
            make.bottom.equalToSuperview().offset(-12.0.Scale)
            make.height.equalTo(4.0.Scale)
            make.width.equalTo(20.0.Scale)
        }
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return bannarModel?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "ClubInfoFSCell", at: index) as! ClubInfoFSCell
        cell.model = bannarModel?[index]
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int) {
            self.pagerControl.currentPage = index
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        var urlString = bannarModel?[index].link ?? ""
        let gid = bannarModel?[index].gid ?? 0
        if gid > 0 {
            let detailVC = ClubGameDetailViewController()
            detailVC.gameId = String(gid)
            UIApplication.topController()?.navigationController?.pushViewController(detailVC, animated: true)
        }else{
            if urlString.count > 0 {
                if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                    urlString = urlString + "?uid=\(uid)"
                }
                let webVC = ClubHiddenBavWebViewController()
                urlString = urlString + "&model=ios"
                webVC.urlString = urlString
                webVC.titleName = "资讯详情"
                UIApplication.topController()?.navigationController?.pushViewController(webVC, animated: true)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class ClubInfoFSCell:FSPagerViewCell {

    var bgImageView = UIImageView()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    
    var model:ClubHomeInfoBannerModel? {
        didSet {
            guard let md = model else { return }
            bgImageView.loadImage(url: md.image)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.layer.shadowColor = UIColor.clear.cgColor
        
        subviews{
            bgImageView.subviews{
                shadowView
            }
        }
        
        bgImageView.contentMode = .scaleAspectFill
        bgImageView.layer.cornerRadius = 8
        bgImageView.layer.masksToBounds = true

        bgImageView.top(8.0.Scale).leading(12.0.Scale).trailing(12.0.Scale).height(162.0.Scale)
        shadowView.fillContainer()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
